package 面试题27_二叉树的镜像;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 10:59 2021/3/14
 * @Description：
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class Solution {
    public TreeNode mirrorTree(TreeNode root) {

        if (root == null) {
            return null;
        }

        mirrorRecur(root);

        return root;
    }

    void mirrorRecur(TreeNode pNode) {
        // 二叉树递归遍历的结束条件
        if (pNode == null) {
            return;
        }

        if (pNode.left == null && pNode.right == null) {
            return;
        }

        // 交换两个子节点
        TreeNode temp = pNode.left;
        pNode.left = pNode.right;
        pNode.right = temp;

        if (pNode.left != null) {
            mirrorRecur(pNode.left);
        }

        if (pNode.right != null) {
            mirrorRecur(pNode.right);
        }
    }
}
